package org.toxsoft.uskat.core.backend.api;

import static org.toxsoft.core.tslib.av.EAtomicType.*;
import static org.toxsoft.core.tslib.av.impl.AvUtils.*;
import static org.toxsoft.core.tslib.av.metainfo.IAvMetaConstants.*;

import org.toxsoft.core.tslib.av.impl.*;
import org.toxsoft.core.tslib.av.metainfo.*;
import org.toxsoft.core.tslib.bricks.events.msg.*;
import org.toxsoft.core.tslib.coll.helpers.*;
import org.toxsoft.core.tslib.gw.gwid.*;
import org.toxsoft.core.tslib.utils.errors.*;
import org.toxsoft.uskat.core.api.clobserv.*;

/**
 * Messages generated by {@link IBaClobs} addon to the frontend.
 *
 * @author hazard157
 */
public interface IBaClobsMessages {

  // FIXME move to BaMsgXxx

  // ------------------------------------------------------------------------------------
  // Message: any change in the CLOBs

  /**
   * The message ID.
   */
  String MSGID_CLOB_CHANGE = "ClobChange"; //$NON-NLS-1$

  /**
   * ID of the option {@link #OPDEF_CLOB_GWID}.
   */
  String OPID_CLOB_GWID = "CrudOp"; //$NON-NLS-1$

  /**
   * {@link GtMessage#args()} option: stores affected CLOB GWID or not specified for {@link ECrudOp#LIST}.
   */
  IDataDef OPDEF_CLOB_GWID = DataDef.create( OPID_CLOB_GWID, VALOBJ, //
      TSID_KEEPER_ID, Gwid.KEEPER_ID, //
      TSID_IS_MANDATORY, AV_FALSE //
  );

  /**
   * Creates the {@link GtMessage} for any change in the classes.
   *
   * @param aClobGwid {@link Gwid} - concrete GWID of kind {@link EGwidKind#GW_CLOB}
   * @return {@link GtMessage} - created instance to send to the frontend
   * @throws TsNullArgumentRtException any argument = <code>null</code>
   */
  static GtMessage makeMessage( Gwid aClobGwid ) {
    TsNullArgumentRtException.checkNull( aClobGwid );
    return new GtMessage( ISkClobService.SERVICE_ID, MSGID_CLOB_CHANGE, //
        OPID_CLOB_GWID, avValobj( aClobGwid ) );
  }

  /**
   * Extracts {@link #OPDEF_CLOB_GWID} value from the message with ID {@link #MSGID_CLOB_CHANGE}.
   *
   * @param aMsg {@link GenericMessage} - the message
   * @return {@link Gwid} - retreived value may be <code>null</code>
   * @throws TsNullArgumentRtException any argument = <code>null</code>
   * @throws TsIllegalArgumentRtException message is not {@link #MSGID_CLOB_CHANGE}
   */
  static Gwid extractClobGwid( GenericMessage aMsg ) {
    TsNullArgumentRtException.checkNull( aMsg );
    TsIllegalArgumentRtException.checkFalse( aMsg.messageId().equals( MSGID_CLOB_CHANGE ) );
    return aMsg.args().getValobj( OPID_CLOB_GWID, null );
  }

}
